package io.wdb.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期处理
 * 
 * @author wdb
 * @email admin@wdb007.com
 * @date 2016年12月21日 下午12:53:33
 */
public class DateUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	/** 时间格式(MM/dd) */
	public final static String DATE_MONTH_DAY="MM/dd";
	public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }
    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }
    
    /**
	 * 获取两者时间差，多少天多少小时多少分钟
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm + (diff % nd % nh % nm > 0 ? 1 : 0);
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    StringBuffer result = new StringBuffer();
	    if(day != 0)  
	    	result.append(day).append("天");
	    if(hour != 0) 
	    	result.append(hour).append("小时");
	    if(day == 0 && hour == 0 && min > 5) 
	    	result = new StringBuffer("不足1小时");
	    if(day == 0 && hour == 0 && min <= 5) 
	    	result = new StringBuffer("不足5分钟");
	    if(day != 0 && hour == 0 && min > 0) {
	    	result.append(1).append("小时");
	    }
	    return result.toString() ;
	}
	
}
